package com.magicsoft.app.wcf;

import org.apache.http.Header;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.boyuan.huijiaoyu.R;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.magicsoft.app.helper.TokenHelper;
import com.magicsoft.app.wcf.listener.PostRecordResponseListener;
import com.magicsoft.constant.Constant;

public class AccountService extends BaseService {

	public AccountService(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	/**
	 * 修改密码
	 * 
	 * @param username
	 *            用户名
	 * @param password
	 *            新密码
	 * @param listener
	 */
	public void changePassword(String username, String password,
			final PostRecordResponseListener listener) {

		RequestParams params = new RequestParams();

		params.put("username", username);
		params.put("password", password);

		// String requestUrl = Constant.EMPLOYEE_PASSWORD;
		String getParamString = "";

		try {//
			getParamString = TokenHelper.FetchColourToken(context, false);
		} catch (Exception e) {
		}

		String requestUrl = Constant.EMPLOYEE_PASSWORD + getParamString;
		Log.i("changePassword", requestUrl);

		AsyncHttpServiceHelper.post(requestUrl, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						// TODO Auto-generated method stub
						super.onSuccess(statusCode, headers, response);

						try {
							Log.i("changePassword", response.toString());

							String code = response.get("code").toString();
							String message = response.get("message").toString();
							if (listener != null)
								if (code.equals("0")) {
									listener.onFinish(message);
								} else {
									listener.onFailed(message);
								}

						} catch (Exception e) {
							if (listener != null)
								listener.onFailed(context
										.getResources()
										.getString(R.string.json_analysis_wrong));// JSON
							// 解析错误
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						// TODO Auto-generated method stub
						super.onFailure(statusCode, headers, responseString,
								throwable);
						if (listener != null)
							listener.onFailed(context
									.getResources()
									.getString(R.string.network_connections_wrong));

					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							Throwable throwable, JSONObject errorResponse) {
						// TODO Auto-generated method stub
						super.onFailure(statusCode, headers, throwable,
								errorResponse);
						try {
							Log.e("checkSMS", errorResponse.toString());

							String code = errorResponse.get("code").toString();
							String message = errorResponse.get("message")
									.toString();

							if (listener != null)
								listener.onFailed(message);

						} catch (Exception e) {
							if (listener != null)
								listener.onFailed(context
										.getResources()
										.getString(R.string.json_analysis_wrong));// JSON
							// 解析错误
						}
					}

				});

	}

}
